<script setup lang="ts" name="MultiInstance">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import {
  isMultiInstanceSupported,
  getMultiInstanceData,
  setLoopCardinalityValue,
  setCompletionConditionValue,
  setCollectionValue,
  setElementVariableValue,
  setAsynchronousBeforeValue,
  setAsynchronousAfterValue,
  setExclusiveValue,
  setRetryTimeCycleValue
} from '../../utils/multiInstance'
import EventEmitter from '@/utils/EventEmitter'

const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const form = ref({
  loopCardinality: '',
  completionCondition: '',
  collection: '',
  elementVariable: '',
  failedJobRetryTimeCycle: '',
  asyncBefore: false,
  asyncAfter: false,
  exclusive: false
})

const showExclusive = computed(() => {
  return form.value.asyncBefore || form.value.asyncAfter
})

const setLoopCardinality = (v) => {
  setLoopCardinalityValue(mapStates.value.getActive(), v)
}

const setCompletionCondition = (v) => {
  setCompletionConditionValue(mapStates.value.getActive(), v)
}

const setCollection = (v) => {
  setCollectionValue(mapStates.value.getActive(), v)
}

const setElementVariable = (v) => {
  setElementVariableValue(mapStates.value.getActive(), v)
}

const setAsynchronousBefore = (v) => {
  setAsynchronousBeforeValue(mapStates.value.getActive(), v)
}

const setAsynchronousAfter = (v) => {
  setAsynchronousAfterValue(mapStates.value.getActive(), v)
}

const setExclusive = (v) => {
  setExclusiveValue(mapStates.value.getActive(), v)
}

const setRetryTimeCycle = (v) => {
  setRetryTimeCycleValue(mapStates.value.getActive(), v)
}

const initData = () => {
  form.value = getMultiInstanceData(mapStates.value.getActive())
}

const eventEmitterListener = () => {
  if (isMultiInstanceSupported(mapStates.value.getActive())) {
    initData()
  }
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  initData()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="multi-instance">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:instance-fill"></svg-icon>
        <h2>多实例</h2>
      </div>
    </template>
    <el-form-item label="循环基数">
      <el-input v-model="form.loopCardinality" clearable @change="setLoopCardinality" />
    </el-form-item>
    <el-form-item label="完成条件">
      <el-input v-model="form.completionCondition" clearable @change="setCompletionCondition" />
    </el-form-item>
    <el-form-item label="集合">
      <el-input v-model="form.collection" clearable @change="setCollection" />
    </el-form-item>
    <el-form-item label="元素变量">
      <el-input v-model="form.elementVariable" clearable @change="setElementVariable" />
    </el-form-item>
    <el-form-item label="异步延续">
      <el-checkbox v-model="form.asyncBefore" label="异步前" @change="setAsynchronousBefore" />
      <el-checkbox v-model="form.asyncAfter" label="异步后" @change="setAsynchronousAfter" />
      <el-checkbox v-if="showExclusive" v-model="form.exclusive" label="异步优先" @change="setExclusive" />
    </el-form-item>
    <el-form-item v-if="showExclusive" label="重试时间周期">
      <el-input v-model="form.failedJobRetryTimeCycle" clearable @change="setRetryTimeCycle" />
    </el-form-item>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        font-weight: bold;
        font-size: 14px;
      }

      .el-button {
        margin-left: 12px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}

.el-form-item {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;

    .el-button.add {
      width: 100%;
      margin: 12px 0 0;
    }
  }
}

.item-content {
  display: flex;
  flex: 1;
  padding: 6px;

  .el-select {
    flex: none;
    width: 90px;

    :deep(.el-input) {
      .el-input__wrapper.is-focus {
        z-index: 2;
      }
    }
  }

  >.el-input,
  .el-date-editor.el-input {
    flex: 1;
    margin-left: -1px;
  }

  :deep(.el-date-editor) {
    flex: 1;
    width: auto;
    margin-left: -1px;

    .el-input__wrapper {
      width: 100%;

      &.is-focus {
        z-index: 2;
      }
    }
  }
}
</style>
